package com.example.gallery;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ImageHandler {
	private DBHelper helper;
	private SQLiteDatabase db;
	private static final String table = "IMAGE";
	private static final String TAG = table+ " Handler";
	
	SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
	long time = System.currentTimeMillis();
	String DATE = format.format(time);
	
	private ImageHandler(Context ctx){
		this.helper = new DBHelper(ctx);
		this.db = helper.getWritableDatabase();
	}
	
	public static ImageHandler open(Context ctx) throws SQLException{
		ImageHandler handler = new ImageHandler(ctx);
		return handler;
	}
	
	public void close(){
		helper.close();
	}
	
//	save images
	public long insert(ImageDTO dto){
		ContentValues values = new ContentValues();
		
		values.put("ID", dto.getID());				//작성자 ID
		values.put("BOARD_NO", dto.getBOARD_NO());	//게시글 번호
		values.put("NAME", dto.getNAME());			//이미지 경로
		Log.i(TAG, "image_path : "+dto.getNAME());
		return db.insert(table, null, values);
	
	}
	
	//글에 저장 되어 있는 이미지 경로 모두 가져오기
	public ArrayList<ImageDTO> getList(int BOARD_NO){
		ArrayList<ImageDTO> list = new ArrayList<ImageDTO>();
		String sql = "SELECT NO, ID, BOARD_NO, NAME FROM IMAGE WHERE BOARD_NO = "+BOARD_NO;
		
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.moveToFirst()){
			do{
				ImageDTO dto = new ImageDTO();
				dto.setNO(cursor.getInt(0));
				dto.setID(cursor.getString(1));
				dto.setBOARD_NO(cursor.getInt(2));
				dto.setNAME(cursor.getString(3));
				list.add(dto);
				Log.i(TAG, "image_path : "+dto.getNAME());
			}while(cursor.moveToNext());
			Log.i(TAG, "# of image: "+cursor.getCount());
		}
		return list;
	}
	
//	detail information of image_no
	public ImageDTO getDetailpath(int no){
		String sql = "SELECT NO, ID, BOARD_NO, NAME FROM IMAGE WHERE NO ="+no+"";
		Log.i(TAG, no+"");
		ImageDTO dto = null;

		Cursor cursor = db.rawQuery(sql, null);
		if(cursor != null){
			cursor.moveToFirst();
				dto = new ImageDTO();
				dto.setNO(Integer.parseInt(cursor.getString(0)));
				dto.setID(cursor.getString(1));
				dto.setBOARD_NO(Integer.parseInt(cursor.getString(2)));
				dto.setNAME(cursor.getString(3));
		}
		
		return dto;
	}
	
	public int update(ImageDTO dto){
		ContentValues values = new ContentValues();
		values.put("NAME", dto.getNAME());
		
/*		db.execSQL("UPDATE BOARD SET " +
								"TITLE = "+dto.getTITLE()+
								", CONTENT = "+dto.getCONTENT()+
								", BOARD_DATE ="+DATE +
							"WHERE NO = "+dto.getNO());
*/		return db.update(table, values, "NO = ?", new String[] {dto.getNO()+""} );
	}
	
//	delete image
	public int delete(int no){
		return 	db.delete(table, "NO = ?", new String[] {no+""});
	}
	

	
}
